import  {createRouter,createWebHashHistory} from 'vue-router'
import store from '@/store'
import {  showFailToast } from 'vant';
import { setToastDefaultOptions} from 'vant';
//设置动画持续时间
setToastDefaultOptions({ duration: 1000 });
//引入路由组件
import Mine from '../views/Mine.vue'
import Home from '../views/Home.vue'
import Search from '../components/home/search/Search.vue'
import Message from '../views/MessageList.vue'
import MsgDetail from '../components/message/MsgDetail.vue'
import OtherHomePage from '../components/OtherHomePage.vue'
import GoodsDetail from '../components/goods/GoodsDetail.vue'
import Single from '../components/home/search/Single.vue'
import Activity from '../components/home/search/Activity.vue'
import PublishPage from "../views/PublishPage.vue"
import TogetherDetail from '../components/TogetherDetail.vue';
import Together from '../views/Together.vue'
import { setTimeout } from 'core-js';
//创建并暴露一个路由器
const router =  createRouter({
    //history模式
    history:createWebHashHistory(),
    routes:[
        //默认路由
        {
            meta:{
                isAuth:false,        //是否需要授权（token）
                title:'首页'
            },
            path:'/',
            component:Home,

        },
        {
            meta:{
                isAuth:false,
                title:'首页'
            },
            name:'hone',
            path:'/home',
            component:Home,
        },
        {
            meta:{
                isAuth:false,
                title:'搜索'
            },
            path:'/search',
            name:'search',
            component:Search,
            redirect: '/search/singleSearch', // 设置默认打开的页面
            children:[
                {
                    meta:{
                        isAuth:false,
                        title:'单品搜索'
                    },
                    path:'singleSearch',
                    name:'singleSearch',
                    component:Single
                },
                {
                    meta:{
                        isAuth:false,
                        title:'活动搜索'
                    },
                    path:'activitySearch',
                    name:'activitySearch',
                    component:Activity
                }
            ]
        },
        {
            meta:{
                isAuth:false,
                title:'登录'
            },
            path:'/login',
            name:'login',
            component:()=>import("../views/login/LoginPage.vue")
        },
        {
            meta:{
                isAuth:false,
                title:'注册'
            },
            path:'/register',
            name:'register',
            component:()=>import("../views/login/RegisterPage.vue")
        },
        {
            meta:{
                isAuth:true,
                title:'发布'
            },
            path:'/publish',
            name:'publish',
            component:PublishPage
        },
        {
            meta:{
                isAuth:true,
                title:'我的'
            },
            path:'/mine',
            component:Mine
        },
        {
            meta:{
                isAuth:true,
                title:'消息'
            },
            path:'/message',
            component:Message,
        },
        {
            meta:{
                isAuth:true,
                title:'会话详情'
            },
            name:'msgDetail',
            path:'/msgDetail/:id/:lastTime',  //使用占位符声明接收params参数
            /* component:MsgDetail, */  //为什么会报错？？？
            component:()=>import("../components/message/MsgDetail.vue")

        },
        {
            meta:{
                isAuth:true,
                title:'他人主页'
            },
            name:'otherHomePage',
            path:'/otherHomePage/:userId',
            component:OtherHomePage,
            
        },

        {
            meta:{
                isAuth:true,
                title:'商品详情',
            },
            name:'goodsDetail',
            path:'/goodsDetial/:goodsId/:userId',
            props:true,
            component:GoodsDetail,
        },
        {
            meta:{
                isAuth:true,
                title:'拼租',
            },
            name:'together',
            path:'/together',
            props:true,
            component:Together
        },
        {
            meta:{
                isAuth:true,
                title:'商品详情',
            },
            name:'togetherDetail',
            path:'/togetherDetail',
            props:true,
            component:TogetherDetail
        }
    
    ]
})

//全局路由守卫
router.beforeEach((to,from,next)=>{
    //需要权限
    if (to.meta.isAuth){
        //————待完善（验证token是否过期）————
        if (localStorage.getItem('token') && store.state.user.isLogined){
            document.title = to.meta.title;
            next(); 
        }
        else{
            //未登录提示
            showFailToast("请先登录")
            setTimeout(()=>{
                router.push('/login')
            },1100)
            
        }
    }
    //不需要权限
    else{
        document.title = to.meta.title;
        next();
    }
    
})

export default router
